#include "calc.h"
#define GET_TOP_SELECTION 2
#if GET_TOP_SELECTION==1
int getTop(char s[], FILE *fp) {
	int ch, i = 0;
	while ((s[0] = ch = getch(fp)) == ' ' || s[0] == '\t')
	;
	s[1] = '\0';
	if (!isdigit(ch) && ch != '.' && ch != '-' && !isalpha(ch))
	return ch;
	i = 1;

	if (isalpha(ch)) {
		while (isalpha(s[i++] = ch = getch(fp))) {

			;
		}
		s[i - 1] = '\0';
		if (ch != EOF)
		ungetch(ch);

		if (strlen(s) > 1)
		return NAME;
		return s[0];
	}

	if (ch == '-') {
		ch = getch(fp);
		if (isdigit(ch) || ch == '.') {
			s[i++] = ch;
		} else {
			if (ch != EOF)
			ungetch(ch);

			return '-';
		}
	}

	if (isdigit(ch)) {
		for (; isdigit(s[i++] = ch = getch(fp));)
		;
	}
	if (ch == '.') {
		for (; isdigit(s[i++] = ch = getch(fp));)
		;
	}
	s[i] = '\0';
	if (ch != EOF)
	ungetch(ch);
	return NUMBER;
}
#else
#include "fileL.h"
#define MAXLINE 100

char line[MAXLINE];
int getTop(char s[], FILE *fp) {
	int ch, i = 0;
	static int li = -1;
	if (li == -1) {
		if (getLine(line, MAXLINE,fp) == 0)
			return EOF;
		li = 0;
	}

	if (line[li] == '\0') {
		if (getLine(line, MAXLINE,fp) == 0)
			return EOF;
		li = 0;
	}

	while ((ch = s[i] = line[li++]) == ' ' || ch == '\t')
		;
	s[1] = '\0';
	i = 0;
	if (!isalnum(ch) && ch != '.' && ch != '-')
		return ch;

	if (isalpha(ch)) {
		while (isalpha(ch = s[++i] = line[li++]))
			;
		li--;
		s[i] = '\0';
		if (i <= 1)
			return s[0];
		else
			return NAME;
	}

	if (ch == '-') {
		while (isdigit(ch = s[++i] = line[li++]))
			;
		if (i == 1)
			return '-';
	}

	if (isdigit(ch)) {
		while (isdigit(ch = s[++i] = line[li++]))
			;
	}

	if (ch == '.') {
		while (isdigit(ch = s[++i] = line[li++]))
			;
	}
	s[i] = '\0';

	li--;
	return NUMBER;
}

#endif
